package test;

import java.util.ArrayList;
import java.util.List;

//全排列：https://leetcode.cn/problems/permutations/
//时间复杂度：O(n!)
public class Permute {
	public List<List<Integer>> ret;
    public List<Integer> path;
    public boolean[] check;   //判断该位置是否被使用过
    public void dfs(int[] nums){
        if(nums.length==path.size()){
            ret.add(new ArrayList(path));
            return ;
        }
        for(int i=0;i<nums.length;i++){
            if(check[i]==false){
            	check[i]=true;
                path.add(nums[i]);
                dfs(nums);
                //恢复现场
                check[i]=false;
                path.remove(path.size()-1);
            }
            else {
            	//写法一：dfs(nums);
            	//写法二：return ;
            }
        }
    }
    public List<List<Integer>> permute(int[] nums) {
        int n=nums.length;
        ret=new ArrayList<>();
        path=new ArrayList();
        check=new boolean[nums.length];
        dfs(nums);
        return ret;
    }

}
